package com.yl.plugin;

import com.yl.plugin.method.base.BaseMethod;
import com.yl.plugin.method.dynamic.SelectSelectiveMethodGenerator;
import com.yl.plugin.model.AdditionalProperty;
import com.yl.plugin.util.Util;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Results;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * mybatis代码生成自定义插件
 *
 * @author liuxubo
 * @date 2023/7/11 22:53
 */
public class AdditionalPlugin extends PluginAdapter {
    private AdditionalProperty additionalProperty;

    private List<BaseMethod> baseMethodList;

    protected SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 如果插件处于有效状态，则为 true。无效插件将不会被调用
     *
     * @param warnings
     * @return
     */
    @Override
    public boolean validate(List<String> warnings) {
        this.additionalProperty = Util.setMbgProperty(properties, context);
        this.baseMethodList = Util.setBaseMethodList();
        baseMethodList.sort(Comparator.comparing(BaseMethod::order));

        return true;
    }

    /**
     * Mapper接口方法扩展
     *
     * @param interfaze
     * @param introspectedTable
     * @return:boolean
     */
    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {
        //Dao接口添加方法SelectSelective
        if (additionalProperty.isEnableSelectSelective()) {
            FullyQualifiedJavaType modelType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
            // 返回类型 List
            FullyQualifiedJavaType returnType = new FullyQualifiedJavaType("List");
            // 设置泛型参数
            returnType.addTypeArgument(modelType);

            // 设置参数类型是对象
            FullyQualifiedJavaType parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
            Parameter parameter = new Parameter(parameterType, "row");

            Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
            importedTypes.add(new FullyQualifiedJavaType("java.util.List"));


            List<Method> interfazeMethods = interfaze.getMethods();

            Method method = new Method("selectSelective");
            method.setAbstract(true);
            method.addParameter(parameter);
            method.setReturnType(returnType);
            interfazeMethods.add(0, method);

            if (additionalProperty.isAnnotatedMapper()) {
                importedTypes.add(new FullyQualifiedJavaType("org.apache.ibatis.annotations.SelectProvider"));
                method.addAnnotation("@SelectProvider(type=" + modelType.getShortName() + "SqlProvider.class, method=\"" + method.getName() + "\")");

                List<String> resultList = new ArrayList<>();
                List<IntrospectedColumn> columnList = introspectedTable.getAllColumns();
                columnList.forEach(column -> {
                    String actualColumnName = column.getActualColumnName();
                    String javaProperty = column.getJavaProperty();
                    String jdbcTypeName = column.getJdbcTypeName();
                    if(introspectedTable.getPrimaryKeyColumns().stream().anyMatch(c -> c.getActualColumnName().equals(actualColumnName))){
                        resultList.add("        @Result(column=\""+ actualColumnName +"\", property=\""+ javaProperty +"\", jdbcType=JdbcType."+ jdbcTypeName +", id=true)");
                    }else {
                        resultList.add("        @Result(column=\""+ actualColumnName +"\", property=\""+ javaProperty +"\", jdbcType=JdbcType."+ jdbcTypeName +")");
                    }
                });
                method.addAnnotation("@Results({\n"  + resultList.stream().collect(Collectors.joining(",\n")) + "\n" +  "    })");
            }
            interfaze.addImportedTypes(importedTypes);

            //排序  dao中方法和xml中顺序保持一致  查询，删除，新增，修改  只需把查询方法放在前面
            Method selectByPrimaryKeyMethod = interfazeMethods.stream().filter(m -> m.getName().contains("selectByPrimaryKey")).findFirst().orElseThrow(() -> new RuntimeException("获取方法为空异常"));
            interfazeMethods.remove(selectByPrimaryKeyMethod);
            interfazeMethods.add(0, selectByPrimaryKeyMethod);
        }

        return true;
    }

    /**
     * MBG扩展xml元素
     */
    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        //Dao接口添加方法SelectSelective
        if (additionalProperty.isEnableSelectSelective()) {
            // 获取xml文件根节点
            XmlElement rootElement = document.getRootElement();
            //添加在selectByPrimaryKey之后
            rootElement.addElement(3, selectSelectiveSQL(introspectedTable));
        }

        return true;
    }


    /**
     * XML文件中生成selectSelective查询sql
     *
     * @param introspectedTable
     */
    private XmlElement selectSelectiveSQL(IntrospectedTable introspectedTable) {
        XmlElement where = new XmlElement("where");
        for (IntrospectedColumn column : introspectedTable.getAllColumns()) {
            String actualColumnName = column.getActualColumnName();
            String javaProperty = column.getJavaProperty();
            String jdbcTypeName = column.getJdbcTypeName();

            XmlElement whereIf = new XmlElement("if"); //$NON-NLS-1$
            whereIf.addAttribute(new Attribute("test", javaProperty + " != null"));
            whereIf.addElement(new TextElement("and " + actualColumnName + " = #{" + javaProperty + ",jdbcType=" + jdbcTypeName + "}"));
            where.addElement(whereIf);
        }

        XmlElement listSelective = new XmlElement("select");
        listSelective.addAttribute(new Attribute("id", "selectSelective"));
        listSelective.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        TextElement text3 = new TextElement("select");
        XmlElement baseInclude = new XmlElement("include");
        baseInclude.addAttribute(new Attribute("refid", "Base_Column_List"));
        TextElement text4 = new TextElement("from " + introspectedTable.getFullyQualifiedTableNameAtRuntime());
        listSelective.addElement(text3);
        listSelective.addElement(baseInclude);
        listSelective.addElement(text4);
        listSelective.addElement(where);

        return listSelective;
    }

    /**
     * 生成自定义添加的Java文件
     *
     * @param introspectedTable
     */
    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable) {
        List<GeneratedJavaFile> answer = new ArrayList<>();

        String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();

        if (additionalProperty.isEnableService()) {
            String serviceTargetProject = additionalProperty.getServiceTargetProject();
            String servicePackage = additionalProperty.getServicePackage();
            if (StringUtils.isEmpty(serviceTargetProject)) {
                System.out.printf("【未配置【serviceTargetProject】，无法生成【%s】表service接口%n", tableName);
                return null;
            }
            if (StringUtils.isEmpty(servicePackage)) {
                System.out.printf("【未配置【servicePackage】，无法生成【%s】表service接口%n", tableName);
                return null;
            }

            String serviceImplTargetProject = additionalProperty.getServiceImplTargetProject();
            String serviceImplPackage = additionalProperty.getServiceImplPackage();
            if (StringUtils.isEmpty(serviceImplTargetProject)) {
                System.out.printf("【未配置【serviceImplTargetProject】，无法生成【%s】表service实现类%n", tableName);
                return null;
            }
            if (StringUtils.isEmpty(serviceImplPackage)) {
                System.out.printf("【未配置【serviceImplPackage】，无法生成【%s】表service实现类%n", tableName);
                return null;
            }

            GeneratedJavaFile generatedJavaFileService = generateServiceInterface(introspectedTable);
            GeneratedJavaFile generatedJavaFileServiceImpl = generateServiceImpl(introspectedTable);
            if (ObjectUtils.isNotEmpty(generatedJavaFileService)) {
                answer.add(generatedJavaFileService);
            }
            if (ObjectUtils.isNotEmpty(generatedJavaFileServiceImpl)) {
                answer.add(generatedJavaFileServiceImpl);
            }
        }

        if (additionalProperty.isEnableController()) {
            String controllerTargetProject = additionalProperty.getControllerTargetProject();
            String controllerPackage = additionalProperty.getControllerPackage();

            String serviceTargetProject = additionalProperty.getServiceTargetProject();
            String servicePackage = additionalProperty.getServicePackage();

            String serviceImplTargetProject = additionalProperty.getServiceImplTargetProject();
            String serviceImplPackage = additionalProperty.getServiceImplPackage();

            if (StringUtils.isEmpty(controllerTargetProject)) {
                System.out.printf("【未配置【controllerTargetProject】，无法生成【%s】表controller类%n", tableName);
                return null;
            }
            if (StringUtils.isEmpty(controllerPackage)) {
                System.out.printf("【未配置【controllerPackage】，无法生成【%s】表controller实现类%n", tableName);
                return null;
            }
            if (StringUtils.isEmpty(serviceTargetProject)) {
                System.out.printf("【未配置【serviceTargetProject】，无法生成【%s】表controller实现类%n", tableName);
                return null;
            }
            if (StringUtils.isEmpty(servicePackage)) {
                System.out.printf("【未配置【servicePackage】，无法生成【%s】表controller实现类%n", tableName);
                return null;
            }

            if (StringUtils.isEmpty(serviceImplTargetProject)) {
                System.out.printf("【未配置【serviceImplTargetProject】，无法生成【%s】表service实现类%n", tableName);
                return null;
            }
            if (StringUtils.isEmpty(serviceImplPackage)) {
                System.out.printf("【未配置【serviceImplPackage】，无法生成【%s】表service实现类%n", tableName);
                return null;
            }

            GeneratedJavaFile generatedJavaFileController = generateController(introspectedTable);
            if (ObjectUtils.isNotEmpty(generatedJavaFileController)) {
                answer.add(generatedJavaFileController);
            }
        }
        return answer;
    }


    /**
     * MBG扩展service方法
     */
    protected GeneratedJavaFile generateServiceInterface(IntrospectedTable introspectedTable) {
        String serviceTargetProject = additionalProperty.getServiceTargetProject();
        String servicePackage = additionalProperty.getServicePackage();

        String baseRecordType = introspectedTable.getBaseRecordType();
        String modelName = baseRecordType.substring(baseRecordType.lastIndexOf(".") + 1);

        String serviceClassName = servicePackage + "." + modelName + "Service";

        FullyQualifiedJavaType service = new FullyQualifiedJavaType(serviceClassName);
        Interface serviceInterface = new Interface(service);
        serviceInterface.setVisibility(JavaVisibility.PUBLIC);

        //添加类注释
        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> serviceInterface.addJavaDocLine(s));

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
        importedTypes.add(new FullyQualifiedJavaType("java.util.List"));
        importedTypes.add(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        importedTypes.add(new FullyQualifiedJavaType("com.github.pagehelper.PageInfo"));
        serviceInterface.addImportedTypes(importedTypes);

        // 执行扩展方法，扩展方法由子类实现
        List<Method> methodList = new ArrayList<>();
        for (BaseMethod baseMethod : baseMethodList) {
            methodList.add(baseMethod.buildMethod(introspectedTable));
        }

        for (Method method : methodList) {
            List<Parameter> parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                importedTypes.add(parameter.getType());
            }
            method.setAbstract(true);
            serviceInterface.addMethod(method);
        }

        return new GeneratedJavaFile(serviceInterface, serviceTargetProject, context.getJavaFormatter());
    }


    /**
     * MBG扩展service实现类
     */
    protected GeneratedJavaFile generateServiceImpl(IntrospectedTable introspectedTable) {
        String servicePackage = additionalProperty.getServicePackage();
        String serviceImplTargetProject = additionalProperty.getServiceImplTargetProject();
        String serviceImplPackage = additionalProperty.getServiceImplPackage();

        String baseRecordType = introspectedTable.getBaseRecordType();
        String modelName = baseRecordType.substring(baseRecordType.lastIndexOf(".") + 1);

        String serviceClassName = servicePackage + "." + modelName + "Service";
        String serviceImplClassName = serviceImplPackage + "." + modelName + "ServiceImpl";

        String mapperFieldType = introspectedTable.getMyBatis3JavaMapperType();

        FullyQualifiedJavaType mapperJavaType = new FullyQualifiedJavaType(mapperFieldType);
        FullyQualifiedJavaType serviceJavaType = new FullyQualifiedJavaType(serviceClassName);
        FullyQualifiedJavaType serviceImplJavaType = new FullyQualifiedJavaType(serviceImplClassName);

        TopLevelClass serviceImplInterface = new TopLevelClass(serviceImplJavaType);

        //添加类注释
        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> serviceImplInterface.addJavaDocLine(s));

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
        importedTypes.add(new FullyQualifiedJavaType("java.util.List"));
        importedTypes.add(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        importedTypes.add(mapperJavaType);
        importedTypes.add(serviceJavaType);
        importedTypes.add(new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired"));
        importedTypes.add(new FullyQualifiedJavaType("org.springframework.stereotype.Service"));
        importedTypes.add(new FullyQualifiedJavaType("com.github.pagehelper.PageHelper"));
        importedTypes.add(new FullyQualifiedJavaType("com.github.pagehelper.PageInfo"));

        serviceImplInterface.addImportedTypes(importedTypes);

        //描述类的作用域修饰符
        serviceImplInterface.setVisibility(JavaVisibility.PUBLIC);

        // 添加实现类
        serviceImplInterface.addSuperInterface(serviceJavaType);
        serviceImplInterface.addAnnotation("@Service");

        String mapperFieldName = StringUtils.uncapitalize(mapperJavaType.getShortName());
        //描述类的成员属性
        Field daoField = new Field(mapperFieldName, mapperJavaType);

        //描述成员属性修饰符
        daoField.setVisibility(JavaVisibility.PRIVATE);
        serviceImplInterface.addField(daoField);
        daoField.addAnnotation("@Autowired");

        // 执行扩展方法，扩展方法由子类实现
        List<Method> methodList = new ArrayList<>();
        for (BaseMethod baseMethod : baseMethodList) {
            methodList.add(baseMethod.buildMethod(introspectedTable));
        }

        for (Method method : methodList) {
            List<Parameter> parameters = method.getParameters();
            for (Parameter parameter : parameters) {
                importedTypes.add(parameter.getType());
            }
            method.addAnnotation("@Override");
//            List<String> javaDocLines = method.getJavaDocLines();
//            if (ObjectUtils.isNotEmpty(javaDocLines)) {
//                javaDocLines.clear();
//            }
            serviceImplInterface.addMethod(method);
        }

        // 添加方法体
        for (Method method : serviceImplInterface.getMethods()) {
            String paramStr = Optional.ofNullable(method.getParameters())
                    .orElse(new ArrayList<>())
                    .stream()
                    .map(Parameter::getName)
                    .collect(Collectors.joining(", "));
            String methodName = method.getName();
            if (methodName.equals("add")) {
                method.addBodyLine("return " + mapperFieldName + "." + "insertSelective" + "(" + paramStr + ");");
            } else if (methodName.equals("delete")) {
                method.addBodyLine("return " + mapperFieldName + "." + "deleteByPrimaryKey" + "(" + paramStr + ");");
            } else if (methodName.equals("update")) {
                method.addBodyLine("return " + mapperFieldName + "." + "updateByPrimaryKeySelective" + "(" + paramStr + ");");
            } else if (methodName.equals("detail")) {
                method.addBodyLine("return " + mapperFieldName + "." + "selectByPrimaryKey" + "(" + paramStr + ");");
            } else if (methodName.equals("list")) {
                method.addBodyLine("return " + mapperFieldName + "." + "selectSelective" + "(" + paramStr + ");");
            } else if (methodName.equals("page")) {
                method.addBodyLine("PageHelper.startPage(pageNum, pageSize);");
                method.addBodyLine("List<" + modelName + "> list = " + mapperFieldName + ".selectSelective(" + method.getParameters().get(0).getName() + ");");
                method.addBodyLine("PageInfo<" + modelName + "> pageInfo = new PageInfo<>(list);");
                method.addBodyLine("");
                method.addBodyLine("return pageInfo;");
            } else {
                method.addBodyLine("return " + mapperFieldName + "." + method.getName() + "(" + paramStr + ");");
            }
        }

        return new GeneratedJavaFile(serviceImplInterface, serviceImplTargetProject, context.getJavaFormatter());
    }


    /**
     * MBG扩展controller方法
     */
    protected GeneratedJavaFile generateController(IntrospectedTable introspectedTable) {
        String controllerTargetProject = additionalProperty.getControllerTargetProject();
        String controllerPackage = additionalProperty.getControllerPackage();
        String servicePackage = additionalProperty.getServicePackage();

        String baseRecordType = introspectedTable.getBaseRecordType();
        String modelName = baseRecordType.substring(baseRecordType.lastIndexOf(".") + 1);

        String serviceClassName = servicePackage + "." + modelName + "Service";
        String controllerClassName = controllerPackage + "." + modelName + "Controller";

        FullyQualifiedJavaType serviceJavaType = new FullyQualifiedJavaType(serviceClassName);
        FullyQualifiedJavaType controllerJavaType = new FullyQualifiedJavaType(controllerClassName);

        TopLevelClass controllerInterface = new TopLevelClass(controllerJavaType);

        //添加类注释
        Util.setClassComment(introspectedTable.getRemarks(), introspectedTable.getContext()).forEach(s -> controllerInterface.addJavaDocLine(s));

        Set<FullyQualifiedJavaType> importedTypes = new TreeSet<>();
        importedTypes.add(new FullyQualifiedJavaType("java.util.List"));
        importedTypes.add(new FullyQualifiedJavaType(introspectedTable.getBaseRecordType()));
        importedTypes.add(serviceJavaType);
        // Mapping注解包
        importedTypes.add(new FullyQualifiedJavaType("org.springframework.web.bind.annotation.*"));
        importedTypes.add(new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired"));
        // 分页类
        importedTypes.add(new FullyQualifiedJavaType("com.github.pagehelper.PageInfo"));

        if (additionalProperty.isEnableSwagger2()) {
            importedTypes.add(new FullyQualifiedJavaType("io.swagger.annotations.Api"));
            importedTypes.add(new FullyQualifiedJavaType("io.swagger.annotations.ApiOperation"));
        }

        controllerInterface.addImportedTypes(importedTypes);

        //描述类的作用域修饰符
        controllerInterface.setVisibility(JavaVisibility.PUBLIC);
        controllerInterface.addAnnotation("@RestController");
        controllerInterface.addAnnotation("@RequestMapping(\"" + "/" + StringUtils.uncapitalize(modelName) + "\")");

        if (additionalProperty.isEnableSwagger2()) {
            controllerInterface.addAnnotation("@Api(tags = \"" + "" + "\")");
        }

        //注入的service接口属性名改为类名，首字母小写
        String serviceFieldName = StringUtils.uncapitalize(serviceJavaType.getShortName());

        //描述类的成员属性
        Field daoField = new Field(serviceFieldName, serviceJavaType);
        //描述成员属性修饰符
        daoField.setVisibility(JavaVisibility.PRIVATE);
        //描述成员属性 的注解
        daoField.addAnnotation("@Autowired");
        controllerInterface.addField(daoField);


        // 执行扩展方法，扩展方法由子类实现
        List<Method> methodList = new ArrayList<>();
        for (BaseMethod baseMethod : baseMethodList) {
            methodList.add(baseMethod.buildMethod(introspectedTable));
        }

        for (Method method : methodList) {
            String methodName = method.getName();

            List<Parameter> parameters = method.getParameters();
            String methodRemake;
            if (methodName.startsWith("insert") || methodName.startsWith("add")) {
                if (parameters.size() == 1) {
                    Parameter parameter = parameters.get(0);
                    importedTypes.add(parameter.getType());
                    parameter.addAnnotation("@RequestBody");
                }

                if (methodName.equals("add")) {
                    methodRemake = "新增";
                } else {
                    methodRemake = methodName;
                }

                method.addAnnotation("@PostMapping(\"/" + methodName + "\")");

            } else if (methodName.startsWith("delete")) {
                for (Parameter parameter : parameters) {
                    importedTypes.add(parameter.getType());
//                    parameter.addAnnotation("@RequestParam(value = \"" + parameter.getName() + "\")");
                }

                if (methodName.equals("delete")) {
                    methodRemake = "删除";
                } else {
                    methodRemake = methodName;
                }

                method.addAnnotation("@GetMapping(\"/" + methodName + "\")");
            } else if (methodName.startsWith("update")) {
                if (parameters.size() == 1) {
                    Parameter parameter = parameters.get(0);
                    importedTypes.add(parameter.getType());
                    parameter.addAnnotation("@RequestBody");
                }

                if (methodName.equals("update")) {
                    methodRemake = "修改";
                } else {
                    methodRemake = methodName;
                }

                method.addAnnotation("@PostMapping(\"/" + methodName + "\")");
            } else if (methodName.startsWith("get") || methodName.startsWith("select") || methodName.startsWith("detail")) {
                for (Parameter parameter : parameters) {
                    importedTypes.add(parameter.getType());
//                    parameter.addAnnotation("@RequestParam(value = \"" + parameter.getName() + "\")");
                }

                if (methodName.equals("detail")) {
                    methodRemake = "详情";
                } else {
                    methodRemake = methodName;
                }

                method.addAnnotation("@GetMapping(\"/" + methodName + "\")");
            } else if (methodName.startsWith("list")) {
                if (parameters.size() == 1) {
                    Parameter parameter = parameters.get(0);
                    importedTypes.add(parameter.getType());
                    parameter.addAnnotation("@RequestBody");
                }

                if (methodName.equals("list")) {
                    methodRemake = "列表查询";
                } else {
                    methodRemake = methodName;
                }

                method.addAnnotation("@PostMapping(\"/" + methodName + "\")");
            } else if (methodName.toLowerCase().contains("page")) {
                //第一参数
                Parameter parameter = parameters.get(0);
                importedTypes.add(parameter.getType());
                parameter.addAnnotation("@RequestBody");

                methodRemake = "分页查询";
                method.addAnnotation("@PostMapping(\"/" + methodName + "\")");
            } else {
                throw new RuntimeException(String.format("异常：非法的方法名称【%s】", methodName));
            }

            if (additionalProperty.isEnableSwagger2()) {
//                List<String> javaDocLines = method.getJavaDocLines();
//                if (ObjectUtils.isNotEmpty(javaDocLines)) {
//                    javaDocLines.clear();
//                }
                method.addAnnotation("@ApiOperation(value = \"" + methodRemake + "\", notes = \"\")");
            }

            controllerInterface.addMethod(method);
        }

        // 添加方法体
        for (Method method : controllerInterface.getMethods()) {
            List<Parameter> parameters = method.getParameters();
            StringBuilder paramBuilder = new StringBuilder();
            if (ObjectUtils.isNotEmpty(parameters)) {
                for (Parameter p : parameters) {
                    paramBuilder.append(p.getName()).append(", ");
                }
                paramBuilder.deleteCharAt(paramBuilder.length() - 1);
                paramBuilder.deleteCharAt(paramBuilder.length() - 1);
            }

            method.addBodyLine("return " + serviceFieldName + "." + method.getName() + "(" + paramBuilder + ");");
        }

        return new GeneratedJavaFile(controllerInterface, controllerTargetProject, context.getJavaFormatter());
    }


    @Override
    public boolean providerGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        //selectSelective 在SqlProvider类中生成动态sql
        Method method = new SelectSelectiveMethodGenerator()
                .buildSql(introspectedTable);
        topLevelClass.addMethod(method);

        return true;
    }
}